package com.hqj.controller;

import com.hqj.common.IsPattern;
import com.hqj.entity.User;
import com.hqj.common.ResultVO;
import com.hqj.entity.User;
import com.hqj.service.UserService;
import com.hqj.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.logging.log4j.ThreadContext.isEmpty;

@Controller
@Api(tags = "登录注册")
public class LoginController {
    @Resource
    private UserService userService;

    @ApiOperation("修改操作")
    @PostMapping("/update2")
    public String update(Long id, String username, String name, String email) {
        System.out.println(id + username + name + email);
        return "main";
    }

    @ApiOperation("查询所有用户")
    @GetMapping("/selectAll")
    public String selectAll(Model model) {
        List<User> users = userService.selectAll();
        users.forEach(System.out::println);
        model.addAttribute("users", users);
        return "main";
    }

    @ApiOperation("登录页面跳转接口")
    @PostMapping("/login")
    public String login(String username, String password, Model model) {
        if ("admin".equals(username) && "123456".equals(password)) {
            List<User> users = userService.selectAll();
            model.addAttribute("users", users);
            return "main";
        }
        return "index";
    }

    @ApiOperation("登录页面跳转判断")
    @PostMapping("/login2")
    public String judgelogin2(String username, String password, Model model) {
        User user = userService.judgelogin(username, password);
        Map<String, String> map = new HashMap<>();
        if (user == null) {
            map.put("msg", "用户名不存在");
            model.addAttribute(map);
            return "index";
        } else if (!password.equals(user.getPassword())) {
            map.put("msg", "密码错误");
            model.addAttribute(map);
            return "index";
        } else if (password.equals(user.getPassword())) {
            map.put("msg", "登录成功");
            model.addAttribute(map);
            return "main";
        }

        return "index";
    }

    @GetMapping("/success")
    public String success() {
        return "success";
    }

    @ApiOperation("注册页面")
    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @ApiOperation("发送邮件注册")
    @PostMapping("/sendEmail")
    public String sendEmail(String username,
                            String password,
                            String code,
                            String email, HttpServletRequest request, Model model) {

        //字母数字密码混合
        boolean matches1 = Pattern.compile(IsPattern.IS_PASSWORD).matcher(password).matches();
        if (!matches1){
            model.addAttribute("msg","密码由6~16位字母和数字组成！");
            return "register";
        }

        //用正则表达式匹配邮箱
        boolean matches = Pattern.compile(IsPattern.IS_EMAIL).matcher(email).matches();
        if(!matches){
            model.addAttribute("msg","邮箱格式不正确！");
        }
        HttpSession session = request.getSession();
        String codes = (String) session.getAttribute("code");
        System.out.println("----------------" + codes);
        //验证码不相等
        if (!code.equals(codes)) {
            model.addAttribute("msg", "验证码错误");

            return "register";
        } else {
            userService.registers(username, password, email, code);
        }
        return "index";
    }

    @ApiOperation("发送邮件验证码")
    @GetMapping("/send")
    @ResponseBody
    public TestVo send(@RequestParam @ApiParam("邮箱") String email,
                       HttpServletRequest request,
                       Model model,
                       HttpServletResponse response
                       ) {
        HttpSession session = request.getSession();
        TestVo testVo = new TestVo();
        testVo.setCode(200);
        String emailSet = "";
        boolean matches = Pattern.compile(IsPattern.IS_EMAIL).matcher(email).matches();
        if(!matches){
           testVo.setMsg("邮箱格式错误");
           return testVo;
        }

        try {
            long attribute = (long) session.getAttribute(email);
            if(System.currentTimeMillis()-attribute<60000){
                testVo.setMsg("1分钟内不能重复发送");
               return testVo;
            }
        } catch (Exception e) {
            emailSet = userService.transmit(email);
            session.setAttribute(email,System.currentTimeMillis());
        }
        session.setAttribute("code", emailSet);
        return testVo;
    }


    @ApiOperation(value = "注销")
    @PostMapping("/logout")
    public String logout(@RequestParam @ApiParam("账户名") String username, Model model) {
        String logout = userService.logout(username);
        model.addAttribute("msg", logout);
        return "index";
    }

    @ApiOperation(value = "搜索用户")
    @GetMapping("/search")
    public String search(@RequestParam @ApiParam("账户名") String name,
                         Model model) {
        User search = userService.search(name);
        model.addAttribute("ssyh", search);
        return "main";
    }

    @ApiOperation(value = "修改用户")
    @PostMapping("/modifyUser")
    public String modifyUser(@RequestParam long id, @RequestParam String username,
                             @RequestParam String email, @RequestParam String name, Model model) {
        String user = userService.modifyUser(id, username, email, name);
        model.addAttribute("xgyh", user);
        return "main";
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("deleteUser")
    public String deleteUser(@RequestParam @ApiParam("用户id") long id, Model model) {
        String deleteUser = userService.deleteUser(id);
        model.addAttribute("scyh", deleteUser);

        return "main";
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/findPage")

    public String findPage(@RequestParam @ApiParam("页数") int page,
                           @RequestParam @ApiParam("条数") int size, Model model) {
        List<User> page1 = userService.findPage(page, size);
        model.addAttribute("fycx", page1);
        return "main";

    }

    @GetMapping("/test2")
    public String test2() {
        return "/page/tes";
    }
}
